﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UploadClient.Interfaces.Upload;

namespace UploadClient
{
    class UploadManager
    {
        public UploadManager(Options options)
        {
            Option = options;
        }

        public Options Option { get; set; }

        // ToDo:
        //ftpUpload.UploadComplete += new UploadCompleteEventHandler(Upload_RunWorkerCompleted);
        //ftpUpload.UploadProgress += new UploadProgressEventHandler(Upload_ProgressChanged);

        /// <summary>
        /// Loads the plugins from folder and initializes them.
        /// </summary>
        /// <param name="host">The IHost.</param>
        /// <returns>A list of IEnumerables.</returns>
        public IEnumerable<IUploadProvider> GetUploadPlugins()
        {
            DummyUploadHost host = new DummyUploadHost();
            List<IUploadProvider> result = new List<IUploadProvider>();

            foreach (string fileName in Directory.GetFiles("./UploadProvider"))
            {
                FileInfo fileInfo = new FileInfo(fileName);

                if (fileInfo.Extension.Equals(".dll"))
                {
                    Assembly pluginAssembly = Assembly.LoadFrom(fileName);

                    foreach (Type pluginType in pluginAssembly.GetTypes())
                    {
                        if (!pluginType.IsAbstract)
                        {
                            Type typeInterface = pluginType.GetInterface("IUploadProvider", true);

                            if (typeInterface != null)
                            {
                                var plugin = (IUploadProvider)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
                                plugin.Init(host);

                                var pluginConfig = Option.PcUploadConfig.Find(item => item.Guid.Equals(plugin.GetConfiguration().Select(guid => guid.Guid)));
                                if (pluginConfig != null)
                                    plugin.Init(host, pluginConfig);

                                result.Add(plugin);
                            }

                            typeInterface = null;
                        }
                    }
                }
            }

            return result;
        }
    }

    public class DummyUploadHost : IUploadHost
    {
        #region IHost Member

        string IUploadHost.GetTempFileDir()
        {
            return "";
        }

        string IUploadHost.GetProgramName()
        {
            return "";
        }

        string IUploadHost.GetExecutingAssemblyLocation()
        {
            return "";
        }

        string IUploadHost.GetExecutingAssemblyDirectoryName()
        {
            return "";
        }

        string IUploadHost.GetProgramVersion()
        {
            return "";
        }

        void IUploadHost.ThrowException(Exception ex)
        {

        }

        void IUploadHost.SetLocalizationRessourceDictionarys(Dictionary<string, Dictionary<string, string>> dictionarys)
        {

        }

        string IUploadHost.GetLocalizationRessourceString(string key)
        {
            return key;
        }

        #endregion
    }
}
